<template>
  <view>
    <!-- 搜索框区域start -->
    <view class="search-box">
      <!-- uni-ni提供的组件 -->
      <uni-search-bar :radius="100" cancelButton="none" @input="input"></uni-search-bar>
    </view>
    <!-- 搜索框区域end -->

    <!-- 搜索结果区域start -->
    <view class="sugg-list" v-if="searchResults.length!==0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索结果区域end -->

    <!-- 搜索关键词历史start -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistoryList"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in historys" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
    <!-- 搜索关键词历史end -->

  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null, //延时器的timerId
        searchWords: '', //搜索关键词
        searchResults: [], //搜索结果列表
        historyList: ['电视', '冰箱', '手机'] //搜索历史
      };
    },

    computed: {
      historys() { //保存逆转过后的搜索历史记录数组
        return [...this.historyList].reverse()
      }
    },

    methods: {
      //搜索框触发事件
      input(e) {
        //清除timer对应的延时器
        clearTimeout(this.timer)
        //重新启动一个延时器 并把timerId赋值给this.timer
        this.timer = setTimeout(() => {
          //如果500ms之内 没有触发新的输入事件  则为搜索关键词赋值
          this.searchWords = e.value //e.value为搜索框输入的内容
          //向后台发起请求
          this.getSearchList()
        }, 500)
      },

      //根据搜索关键词 向后台发送数据
      async getSearchList() {
        if (this.searchWords === '') {
          this.searchResults = []
          return
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.searchWords
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        //查询到结果后 将搜索关键词保存到搜索历史当中
        this.saveHistory()
        this.searchResults = res.message
      },

      //保存关键字到搜索的历史 
      saveHistory() {
        //将搜索历史数组转换成Set对象
        const set = new Set(this.historyList)
        //删除与搜索关键词重复的数据
        set.delete(this.searchWords)
        //添加该搜索关键词
        set.add(this.searchWords)
        //将Set对象转换成数组
        this.historyList = Array.from(set)
        //将uni.setStorageSync方法将搜索历史记录持久化存储到本地
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },

      //点击结果 跳转到详情页面
      gotoDetail(goods_id) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
        })
      },
      
      //清空搜索历史记录
      cleanHistoryList(){
        this.historyList=[]
        uni.setStorageSync('kw','[]')
      },
      
      //点击跳转到商品列表页面
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + item
        })
      }
    },
    
    onLoad(){
      this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        //设置文字不允许换行
        white-space: nowrap;
        //溢出部分隐藏
        overflow: hidden;
        //文本溢出后 使用...代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1ps solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
